/*
 * location.java
 *
 * Copyright (C) 2008 Paul Nettleship
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */
package staticTrailExplorer;

/**
 *
 * @author codeabuer
 */
public class location
   {
   /* Think about making these doubles to avoid zoom rounding! */
   private int pixelX;
   private int pixelY;
   private int zoom;
   public static final int minZoom = 0;
   public static final int maxZoom = 14;
   
   public location(location loc)
      {
      zoom = loc.zoom;
      pixelX = loc.pixelX;
      pixelY = loc.pixelY;
      }

   public location(double x, double y, int zoom)
      {
      this.zoom = zoom;
      pixelX = (int)x;
      pixelY = (int)y;
      }

   public void set(int x, int y, int z)
      {
      pixelX = x;
      pixelY = y;
      zoom = z;
      }

   public void move(int x, int y)
      {
      pixelX += x;
      pixelY += y;
      }

   public void setZoom(int z)
      {
      if ((z <= maxZoom) && (z > minZoom))
         {
         pixelX *= Math.pow(2, z - zoom);
         pixelY *= Math.pow(2, z - zoom);
         zoom = z;
         }
      }

   public int getPixelX(tileMap map)
      {
      return (int) (pixelX * Math.pow(2, map.getScreenRef().zoom - zoom));
      }

   public int getPixelY(tileMap map)
      {
      return (int) (pixelY * Math.pow(2, map.getScreenRef().zoom - zoom));
      }

   public int getDisplayX(tileMap map)
      {
      int x = (int) (pixelX * Math.pow(2, map.getScreenRef().zoom - zoom));
      return x -map.getScreenRef().pixelX;
      }

   public int getDisplayY(tileMap map)
      {
      int y = (int) (pixelY * Math.pow(2, map.getScreenRef().zoom - zoom));
      return y -map.getScreenRef().pixelY;
      }

   public int getTileOrigX(tileMap map)
      {
      int w = map.tileWidth;
      int x = (int) (pixelX * Math.pow(2, map.getScreenRef().zoom - zoom));
      int offset = (x/w);
      offset = x - (offset*w);
      return x - offset - map.getScreenRef().pixelX;
      }

   public int getTileOrigY(tileMap map)
      {
      int h = map.tileHeight;
      int y = (int) (pixelY * Math.pow(2, map.getScreenRef().zoom - zoom));
      int offset = (y/h);
      offset = y - (offset*h);
      return y - offset - map.getScreenRef().pixelY;
      }

   public int getTileX(tileMap map)
      {
      return pixelX / map.tileWidth;
      }

   public int getTileY(tileMap map)
      {
      return pixelY / map.tileHeight;
      }

   public int getZoom()
      {
      return zoom;
      }

   public String getName(tileMap map)
      {
      String hdr = map.name.replaceAll("[\\\\\\./_]","_");
      return hdr + "_" + getTileX(map) + "_" + getTileY(map) + "_" + zoom + ".png";      
      }

}
